﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Text;
using Maticsoft.DBUtility;
using Utils;
using System.Collections.Specialized;
using Maticsoft.Model;

public partial class UserControls_treemore2 : FBasePage
{
    // Fields
    public BaseUserControl baseuser = new BaseUserControl();

    protected bool buy = true;
    protected int buycount = 0;
    protected string buyurl = string.Empty;
    protected Team CurrentTeam = null;
    protected long curtimefix = 0L;
    private string detail = "";
    protected int detailcount = 0;
    protected long difftimefix = 0L;

    protected int imageindex = 0;
    protected int offset = 0;
    protected NameValueCollection order = new NameValueCollection();
    public Maticsoft.BLL.Order orderbll = new Maticsoft.BLL.Order();
    protected int ordercount = 0;
    public List<Order> orderlist = new List<Order>();
    public List<Team> otherteams = new List<Team>();
    protected bool over = false;
    protected DateTime overtime = DateTime.Now;
    protected Partner part = new Partner();
    protected NameValueCollection partner = new NameValueCollection();
    protected int size = 0;
    public int sortorder = 0;
    protected TeamState state = TeamState.none;
    public string strtitle = "";
    protected NameValueCollection team = new NameValueCollection();
    protected int teamid = 0;
    protected string teamstatestring = string.Empty;
    protected int twoline = 0;
    protected NameValueCollection user = new NameValueCollection();
    public Maticsoft.BLL.UserInfo userbll = new Maticsoft.BLL.UserInfo();
    public UserInfo usermodel = new UserInfo();
    public WebSiteHelper wshelp = new WebSiteHelper();

    // Methods
    public bool isover(Team model)
    {
        this.state = base.GetState(model);
        if ((this.state == TeamState.fail) || (this.state == TeamState.successtimeover))
        {
            this.over = true;
            if (model.Close_time.HasValue)
            {
                this.overtime = model.Close_time.Value;
            }
            else
            {
                this.overtime = model.End_time;
            }
        }
        return this.over;
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        this.teamid = Helper.GetInt(base.Request["id"], 0);
        if (this.teamid > 0)
        {
            this.CurrentTeam = new Maticsoft.BLL.Team().GetModel(this.teamid);
        }
        else
        {
            this.CurrentTeam = base.CurrentTeam;
        }
        if (this.CurrentTeam != null)
        {
            if ((this.CurrentTeam != null) && (base.ASSystem.Description == string.Empty))
            {
                this.Page.Header.Controls.Add(new LiteralControl("<meta name=\"description\" content=\"" + this.CurrentTeam.Summary.Replace("\r\n", "") + "\" />\r\n"));
            }
            this.orderlist = this.orderbll.GetByUserid(this.userbll.Getuserid(base.UserName), this.CurrentTeam.Id);
            if (this.CurrentTeam.Buyonce == "Y")
            {
                if ((this.CurrentTeam.Now_number < this.CurrentTeam.Max_number) && (this.orderlist.Count > 0))
                {
                    this.SetError("您已经成功购买了本单产品，请勿重复购买，快去关注一下其他产品吧！");
                }
            }
            else if (((this.CurrentTeam.Buyonce == "N") && (this.CurrentTeam.Now_number < this.CurrentTeam.Max_number)) && ((this.orderlist.Count != 0) && (this.orderlist.Count >= this.CurrentTeam.Per_number)))
            {
                this.SetError("您购买本单产品的数量已经达到上限，快去关注一下其他产品吧！");
            }
        }
        if (this.CurrentTeam == null)
        {
            base.Response.End();
        }
        else
        {
            this.UpdateView();
        }
    }

    public string type(Team model)
    {
        string str = "";
        if (model.Team_type == "seconds")
        {
            str = "今日秒杀";
        }
        if (model.Team_type == "goods")
        {
            str = "今日热销";
        }
        if (model.Team_type == "normal")
        {
            str = "今日团购";
        }
        return str;
    }

    public void UpdateView()
    {
        int sideteam = 0;
        if (base.ASSystem != null)
        {
            sideteam = base.ASSystem.Sideteam;
            if (sideteam != 0)
            {
                string str = " 1=1  ";
                if (base.CurrentCity != null)
                {
                    str = string.Concat(new object[] { str, "  and (City_id=", base.CurrentCity.Id, " or city_id=0)" });
                }
                str = str + " and Team_type='normal' and Begin_time<='" + DateTime.Now.ToString() + "' and end_time>='" + DateTime.Now.ToString() + "' ";
                DataTable dt = DbHelperSQL.SelectByFilter("*", "", "sort_order desc,Begin_time desc,id desc", string.Concat(new object[] { "(select top ", sideteam, " * from team where ", str, " order by  sort_order desc,Begin_time desc,id desc ) as t1" }));
                this.otherteams = new Maticsoft.BLL.Team().DataTableToList(dt);
                if (base.CurrentTeam != null)
                {
                    this.blockask1.UpdateView(base.CurrentTeam, true);
                }
            }
        }
    }

}